package edu.upf.opencities.web.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import edu.upf.opencities.web.action.BaseAction;

public class Utils extends BaseAction {

	/**
	 * 
	 */
	private static Utils util = null;

	public static synchronized Utils getInstance() {
		if (null == util) {
			util = new Utils();
		}
		return util;
	}

	private static final long serialVersionUID = 1L;

	public String returnError(String output) {
		HttpServletRequest servletRequest = getRequest();
		servletRequest.setAttribute("error", "yes");
		servletRequest.setAttribute("errorDescription", output);
		return ERROR;
	}

	public String inputStreamAsString(InputStream stream) throws IOException {
		BufferedReader BR = new BufferedReader(new InputStreamReader(stream));
		StringBuilder SB = new StringBuilder();
		String line1 = null;

		while ((line1 = BR.readLine()) != null) {
			SB.append(line1 + "\n");
		}

		BR.close();
		return SB.toString();
	}
	
	public String stringToUTF(String s){
		byte[] b = s.getBytes();
		String r="";
		for(int i=0; i<b.length; i++){
			r+="%"+Integer.toHexString(b[i]);
		}
		return r.replace("ffffff", "");
		
	}

}
